package com.StarStudio.DAO;


import com.ruoyi.common.core.Entity.Exchange;
import com.ruoyi.common.core.Entity.Login;
import com.ruoyi.system.api.domain.SysUser;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface ExchangeDAO {

    List<Exchange> findExchangeList(Exchange exchange);

    @Select("select * from exchange where exchange_id = #{id}")
    Exchange findByExchange_id(long id);

    @Select("select * from exchange where exchange_user_id = #{id}")
    List<Exchange> findByExchange_user_id(long id);

    @Delete("delete from exchange where exchange_id = #{id}")
    void deleteExchangeById(long id);

    @Insert("insert into exchange values (#{exchange_id},#{order_id},#{exchange_user_id},#{store_id},#{goods_id},#{goods_item_id},#{coupons_id},#{coupons_item_id},#{exchange_status},#{exchange_store},#{use_time},#{link})")
    void addExchange(Exchange exchange);

    void updateExchange(Exchange exchange);

    @Select("select count(*) from exchange")
    long findAllRecordNumber();
    @Delete("delete from exchange where order_id = #{order_id} and goods_item_id = #{goods_item_id}")
    void deleteExchangeByGoodsCondition(@Param("order_id") long order_id,@Param("goods_item_id") long goods_item_id);

    @Delete("delete from exchange where order_id = #{order_id} and coupons_item_id = #{coupons_item_id}")
    void deleteExchangeByCouponsCondition(@Param("order_id") long order_id,@Param("coupons_item_id") long coupons_item_id);

    @Update("update exchange set exchange_status = #{status},exchange_store = #{exchange_store}, use_time = #{use_time} where coupons_item_id = #{coupons_item_id}")
    void updateExchangeStatus(@Param("coupons_item_id") long coupons_item_id, @Param("status") String status,@Param("exchange_store") String exchange_store,@Param("use_time") Timestamp use_time);

    @Update("update exchange set exchange_status = #{status},exchange_store = #{exchange_store}, use_time = now(),link = #{link} where coupons_item_id = #{coupons_item_id}")
    void updateExchangeStatusAndLink(@Param("coupons_item_id") long coupons_item_id, @Param("status") String status,@Param("exchange_store") String exchange_store,@Param("link") String link);


    @Update("update exchange set exchange_status = #{status} where coupons_item_id = #{coupons_item_id}")
    void updateExchangeStatusToExpired(@Param("coupons_item_id") long coupons_item_id, @Param("status") String status);


    @Select("select coupons_item_id from exchange where coupons_item_id > 0 and exchange_status = #{exchange_status}")
    List<Long> findUnExpiredCouponsItemIds(String exchange_status);

    @Select("select * from exchange where coupons_item_id = #{coupons_item_id}")
    Exchange findByCoupons_item_id(long coupons_item_id);

    @Select("select * from sys_user where user_name = #{username} and password = #{password}")
    SysUser findSusUserByUsernameAndPassword(Login login);

    @Select("select * from exchange where coupons_item_id > 0 and exchange_store = #{exchangeStoreName}")
    List<Exchange> findByCouponsIdAndExchangeStore(String exchangeStoreName);

    @Select("select * from exchange where coupons_item_id > 0 and exchange_store = #{exchangeStoreName} and date_format(use_time,'%Y-%m-%d') = #{today}")
    List<Exchange> findByCouponsIdAndExchangeStoreToday(@Param("exchangeStoreName") String exchangeStoreName,@Param("today") String today);
}
